import { useRequest } from "ahooks"

import { getData } from "./request"

export default function useNewDictData<U = DictDataComb>(
  dictType?: number,
  mapper?: (value: Readonly<DictDataComb>, index: number, self: DictDataComb[]) => U,
  options?: {
    refreshDeps?: React.DependencyList
    cacheKey?: string
    cacheTime?: number
  }
) {
  const { data = [] } = useRequest(
    async () => {
      const { data } = await getData<DictDataComb[], Partial<Pick<DictDataEntity, "dictType">>>(
        "/account/common/Bs/api/BasicData/getDictByType.htm",
        { dictType }
      )
      if (mapper) {
        return data.map(mapper) as U[]
      } else {
        return data as U[]
      }
    },
    {
      ...options
    }
  )

  return [data]
}

// 其他存储方式
interface DictDataEntity {
  dictType: number
  guid: string
  code: string
  name: string
}

interface DictDataEntityExpand {
  value?: string
  remark?: string
  picUrl?: string
  picUrl2?: string
  parentGUID?: string
  childDict?: unknown
}

type DictDataComb = DictDataEntity & DictDataEntityExpand
